from web.models import Account_Admin,Account_Admin_Icon
from django.http import JsonResponse
from web import Success_Error_Dict
from django import forms
from django.forms.models import model_to_dict


class Account_Admin_Form(forms.ModelForm):
    class Meta:
        model = Account_Admin
        fields = '__all__'


# 创建账号
def create_account(request):
    if request.method == 'POST':
        form = Account_Admin_Form(data = request.POST)
        if form.is_valid():
            form.save()
            response = Success_Error_Dict.Success_Dict
            response['data'] = form.cleaned_data
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        else:
            print("创建账号错误原因:", form.errors)
            return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
    else:
        return JsonResponse(Success_Error_Dict.Request_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                            safe = False, content_type = Success_Error_Dict.content_type_dict['json'])


# 查询账号
def select_account(request):
    if request.method == 'GET':
        Select_Type = request.GET.get('Select_Type', '0')  # 查询类型
        if Select_Type == '0':
            # 查询所有账号
            AccountAll = [model_to_dict(account) for account in Account_Admin.objects.all()]
            response = Success_Error_Dict.Success_Dict
            response['data'] = AccountAll
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        elif Select_Type == '1':
            # 查询账号(通过账号查询)
            Account = Account_Admin.objects.filter(user_account__contains = request.GET.get("select_params", '18171759943')).first()
            if Account is None:
                return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                    safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
            response = Success_Error_Dict.Success_Dict
            response['data'] = model_to_dict(Account)
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        elif Select_Type == '2':
            # 查询账号(通过用户名查询)
            Account = Account_Admin.objects.filter(user_name__contains = request.GET.get("select_params", 'admin')).first()
            if Account is None:
                return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                    safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
            response = Success_Error_Dict.Success_Dict
            response['data'] = model_to_dict(Account)
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        elif Select_Type == '3':
            # 查询账号(通过平台查询)
            Account = Account_Admin.objects.filter(platform_name__contains = request.GET.get("select_params", '游戏'))
            if Account is None:
                return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                    safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
            response = Success_Error_Dict.Success_Dict
            response['data'] = [model_to_dict(account) for account in Account]
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        else:
            return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
    else:
        return JsonResponse(Success_Error_Dict.Request_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                            safe = False, content_type = Success_Error_Dict.content_type_dict['json'])


# 修改(编辑账号)
def edit_account(request):
    if request.method == 'POST':
        user_id = request.POST.get('user_id', '')
        if user_id != '':
            Account = Account_Admin.objects.filter(id= user_id).first()
            if Account is None:
                return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                    safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
            else:
                form = Account_Admin_Form(data = request.POST, instance = Account)
                if form.is_valid():
                    form.save()
                    response = Success_Error_Dict.Success_Dict
                    response['data'] = form.cleaned_data
                    return JsonResponse(response, status = Success_Error_Dict.status_code_list[0],
                                        safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
                else:
                    print("修改账号错误原因:", form.errors)
                    return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                        safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
        else:
            return JsonResponse(Success_Error_Dict.Param_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
    else:
        return JsonResponse(Success_Error_Dict.Request_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                            safe = False, content_type = Success_Error_Dict.content_type_dict['json'])

# 删除账号
def delete_account(request):
    if request.method == 'POST':
        user_id = request.POST.get('user_id', '')
        if user_id != '':
            Account = Account_Admin.objects.filter(id= user_id).first()
            if Account is None:
                return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                    safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
            Account.delete()
            return JsonResponse(Success_Error_Dict.Success_Dict, status = Success_Error_Dict.status_code_list[0],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
    else:
        return JsonResponse(Success_Error_Dict.Request_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                            safe = False, content_type = Success_Error_Dict.content_type['json'])


# 上传图片
def account_admin_upload(request):
    if request.method == 'POST':
        try:
            file_name = request.FILES.get('file', '')
            file = request.FILES['file']
            update_data = Account_Admin_Icon.objects.create(ico_name = file_name, ico_url = file)
            response = Success_Error_Dict.Success_Dict
            response['data'] =str(model_to_dict(update_data)['ico_url'])
            return JsonResponse(response, status = Success_Error_Dict.status_code_list[0], content_type = Success_Error_Dict.content_type_dict['json'], safe = False)
        except Exception as e:
            return JsonResponse(Success_Error_Dict.Error_Dict, status = Success_Error_Dict.status_code_list[1],
                                safe = False, content_type = Success_Error_Dict.content_type_dict['json'])
    else:
        return JsonResponse(Success_Error_Dict.Request_Error_Dict, status = Success_Error_Dict.status_code_list[1],
                            safe = False, content_type = Success_Error_Dict.content_type_dict[1])